# coding=utf-8

"""
    Created by Kalyter on 2017-12-30.
"""

from learning.scientifc import plot
from learning.scientifc.jobs.job import Job
from learning.scientifc.readers.reader import ReaderFactory


class PlotJob(Job):

    name = '绘图作业'

    def __init__(self, app):
        super().__init__(app)
        self.factory = ReaderFactory(app)
        self._heat_data_filename = self.app.config.get('HEAT_DATA_FILENAME')

    def run(self):
        reader = self.factory.create(self._heat_data_filename)
        reader.load('Sheet3')
        x_titles = reader.row_values(0)[1:]
        y_titles = []
        data = []

        for i in range(1, reader.nrows):
            row = reader.row_values(i)
            y_titles.append(row[0])
            row_data = []
            for j in row[1:]:
                try:
                    row_data.append(float(j))
                except ValueError:
                    row_data.append(0.0)
            data.append(row_data)

        plot.simple_heat_map(data, x_titles, y_titles)
